<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1. bootstrap.css/bootstrap.min.css -->
    <link rel="stylesheet" href="/dev/plugins/bootstrap/css/bootstrap.css">
    <style>
        #nav_img{
            width: 50px;
            height: 50px;
            border-radius: 50%;
            padding: 2px;
        }
        img{
            width: 150px;
            height: 150px;
            border-radius: 50%;
            padding: 2px;
        }
    </style>
</head>
<body>
<header id="header">
<nav class="navbar navbar-inverse" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#example-navbar-collapse">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/dev/views/index.html">小米商城</a>
        </div>
        <div class="collapse navbar-collapse" id="example-navbar-collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#" data-toggle="modal" data-target="#myModal">登录</a></li>
                <li><a href="#">admin</a></li>
                <li><a href="/dev/views/index.html">首页</a></li>
                <li><a href="#">注册</a></li>
                <li class="active"><a href="/dev/views/profile.html">个人中心</a></li>
                <li><a href="/dev/views/shopping.html">热门商品</a></li>
                <li><a href="/dev/views/cart.html"><span class="glyphicon glyphicon-shopping-cart"></span>&nbsp; 购物车</a></li>
                <li><a href="#" onclick="quit()">安全退出</a></li>
                <li><img id="nav_img" src="/dev/imgs/u0.png"></li>
            </ul>
        </div>
    </div>
</nav>
</header>
<div class="container">
    <!-- 个人信息头部 -->
    <div class="row">
        <div class="col-md-4 text-center">
            <img id="userimg" src="/dev/imgs/u0.png" class="img" alt="头像">
            <h3>用户</h3>
            <p>admin</p>

        </div>

        <div class="col-md-8">
            <!-- 导航标签 -->
            <ul class="nav nav-tabs">
                <li class="active"><a href="#info" data-toggle="tab">基本信息</a></li>
            </ul>

            <!-- 标签内容 -->
            <div class="tab-content">
                <!-- 基本信息 -->
                <div class="tab-pane active" id="info">
                    <form class="form-horizontal" style="margin-top:20px;">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">姓名</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" value="用户">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">邮箱</label>
                            <div class="col-sm-10">
                                <input type="email" class="form-control" value="admin">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">保存修改</button>
                            </div>
                        </div>
                    </form>
                </div>



                <!-- 在地址管理面板中添加以下模态框 -->

                <ul class="breadcrumb">
                    <li class="active">收货地址</li>
                </ul>

                <div class="modal fade" id="addressModal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title" id="addressModalLabel">新增地址</h4>
                            </div>
                            <div class="modal-body">
                                <form id="addressForm">
                                    <div class="form-group">
                                        <label>收件人</label>
                                        <input type="text" class="form-control" id="name" required>
                                    </div>
                                    <div class="form-group">
                                        <label>联系电话</label>
                                        <input type="tel" class="form-control" id="phone" required>
                                    </div>
                                    <div class="form-group">
                                        <label>详细地址</label>
                                        <textarea class="form-control" id="detail" rows="3" required></textarea>
                                    </div>
                                    <div class="checkbox">
                                        <label>
                                            <input type="checkbox" id="isDefault"> 设为默认地址
                                        </label>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                <button type="button" class="btn btn-primary" onclick="saveAddress()">保存</button>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 修改地址列表部分的HTML -->
                <div class="list-group address-list">
                    <!-- 动态内容将通过JavaScript填充 -->
                </div>

                <!-- 修改新增地址按钮 -->
                <button class="btn btn-xs btn-primary pull-right" onclick="openAddModal()">新增地址</button>
            </div>
        </div>
    </div>
</div>

<!-- 复用脚本 -->
<script src="/dev/plugins/jquery/jquery.min.js"></script>
<script src="/dev/plugins/bootstrap/js/bootstrap.js"></script>
<script>
    //登录的按钮的单击事件
    function login(){
        let email = $("#email").val();
        let code = $("#code").val();
        let email_span = document.getElementById("email_span");
        let code_span = document.getElementById("code_span");
        //第二个参数是json对象的格式
        $.post('/dev/user',{email:email,code:code},function(res){
            if(res.code=="200"){
                //登录是成功的
                console.log("login-success");
                //获取server返回的token
                let token = res.data;
                //console.log(token
                //保存到本地存储中 - H5 - 存储的数据的格式也是key-value的格式
                localStorage.setItem("token",token);
                window.location='/dev/views/index.html';

            }else if(res.code=="404"){
                email_span.style.display="";
                code_span.style.display="none";
            }else{
                code_span.style.display="";
            }
        })
    }

    //发送验证码的事件
    function send_code(){
        //1. 获取email文本框输入的邮箱
        //js写法
        //let email = document.getElementById("email").value;

        //jquery写法 - jquery是js的框架
        let email = $("#email").val();
        //alert(email)
        //通过ajax方式 - 将email发送到后端
        //发送get请求
        //1. 第一个参数是url地址.  参数是?key1=value1&key2=value
        //2. 第二个参数是一个函数,函数res,代表的是后端返回过来的结果
        //获取邮箱的错误提示的span对象
        let email_span = document.getElementById("email_span");

        $.get(`/dev/user?email=${email}`,function(res){//jquery会自动将这个json字符串->json对象
            //json对象 - 通过json对象.key获取json对象的valu
            //if(res.code)
            if(res.code=="404"){
                //邮箱不存在
                //通过js操作样式,让它再次显示出来
                email_span.style.display="";
            }else{
                email_span.style.display="none";
                //验证码是发送成功
                alert("验证码已经发送到您的邮箱!"+res.data)
            }
        })
    }


    //退出登录
    function quit(){
        localStorage.removeItem("token");
        window.location.href="/dev/views/index.html";
    }

    $(function(){
        $.ajax({
            url:'/dev/cart',
            type:'get',
            headers:{
                "token":localStorage.getItem("token")
            },
            success:function(res){
                if(res.code=="500" || res.code=="501"){
                }else if(res.code=="200"){
                    let arr = res.data;
                    let nav_img  = document.getElementById("nav_img");
                    let userimg  = document.getElementById("userimg");
                    nav_img.src= "/dev/imgs/"+arr[0].headerImg;
                    userimg.src= "/dev/imgs/"+arr[0].headerImg;
                }
            }
        })
    });



    <!-- 在body末尾的script标签内添加以下代码 -->
        // 地址数据初始化
        let addresses = JSON.parse(localStorage.getItem('addresses')) || [];

        // 页面加载时渲染地址列表
        $(function(){
        renderAddressList();
    });

        // 渲染地址列表
        function renderAddressList() {
        const $list = $('.address-list');
        $list.empty();

        addresses.forEach((address, index) => {
        $list.append(`
                <div class="list-group-item" data-index="${index}">
                    <div class="pull-right">
                        <button class="btn btn-xs btn-default edit-btn" onclick="openEditModal(${index})">编辑</button>
                        <button class="btn btn-xs btn-danger delete-btn" onclick="deleteAddress(${index})">删除</button>
                    </div>
                    <h5>${address.isDefault ? '默认地址' : '地址' + (index+1)}</h5>
                    <p>${address.detail}</p>
                    <p>${address.name} ${address.phone}</p>
                </div>
            `);
    });
    }

        // 新增地址弹窗
        function openAddModal() {
        $('#addressForm')[0].reset();
        $('#addressModalLabel').text('新增地址');
        $('#addressModal').modal('show');
    }

        // 编辑地址弹窗
        function openEditModal(index) {
        const address = addresses[index];
        $('#addressForm')[0].reset();
        $('#name').val(address.name);
        $('#phone').val(address.phone);
        $('#detail').val(address.detail);
        $('#isDefault').prop('checked', address.isDefault);
        $('#addressModalLabel').text('编辑地址');
        $('#addressModal').data('index', index).modal('show');
    }

        // 保存地址（新增/编辑）
        function saveAddress() {
        const index = $('#addressModal').data('index');
        const address = {
        name: $('#name').val().trim(),
        phone: $('#phone').val().trim(),
        detail: $('#detail').val().trim(),
        isDefault: $('#isDefault').prop('checked')
    };

        // 简单验证
        if (!address.name || !address.phone || !address.detail) {
        alert('请填写完整信息');
        return;
    }

        if (address.isDefault) {
        addresses.forEach(a => a.isDefault = false);
    }

        if (typeof index === 'number') {
        addresses[index] = address;
    } else {
        addresses.push(address);
    }

        localStorage.setItem('addresses', JSON.stringify(addresses));
        $('#addressModal').modal('hide');
        renderAddressList();
    }

        // 删除地址
        function deleteAddress(index) {
        if (confirm('确定要删除该地址吗？')) {
        addresses.splice(index, 1);
        localStorage.setItem('addresses', JSON.stringify(addresses));
        renderAddressList();
    }
    }
</script>
</body>
</html>